package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.ProjectCollectOriginal;
import com.isunland.managesystem.entity.SimpleTreeListParams;
import com.isunland.managesystem.entity.ZTreeNode;
import com.isunland.managesystem.entity.ZTreeNodeListOriginal;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.Date;

/**
 * 对象功能: 采购申请查询
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/16 11:02
 */
public class BuyApplyQueryFragment extends BaseFragment {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.BuyApplyQueryFragment.EXTRA_CONTENT";

  private final int BEGINREG_DATE = 1;
  private final int ENDREG_DATE = 2;
  private final int REQUEST_PROJECT = 3;
  private final int REQUEST_KIND = 4;
  private final int REQUEST_STATUS = 5;
  private int REQUEST_BUYPURPOSE = 6;

  @BindView(R.id.tv_beginregDate) TextView tvBeginregDate;
  @BindView(R.id.ib_beginregDate) ImageView ibBeginregDate;
  @BindView(R.id.tv_endregDate) TextView tvEndregDate;
  @BindView(R.id.ib_endregDate) ImageView ibEndregDate;
  @BindView(R.id.tv_project) TextView tvProject;
  @BindView(R.id.ib_project) ImageView ibProject;
  @BindView(R.id.tv_materielKind) TextView tvMaterielKind;
  @BindView(R.id.ib_materielKind) ImageView ibMaterielKind;
  @BindView(R.id.tv_dataStatus) TextView tvDataStatus;
  @BindView(R.id.ib_dataStatus) ImageView ibDataStatus;
  @BindView(R.id.tv_buyPurpose) TextView tvBuyPurpose;
  @BindView(R.id.ib_buyPurpose) ImageView ibBuyPurpose;

  private String beginbuyappDate;
  private String endbuyappDate;
  private String projectId;
  private String projectName;
  private String materialKindCode;
  private String materialKindName;
  private String buyappStatusName;
  private String buyappStatus;
  private String buyPurposeName;
  private String buyPurposeCode;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.query);
    ExtraValueContent queryConten =
        (ExtraValueContent) getArguments().getSerializable(EXTRA_CONTENT);
    if (queryConten != null) {
      beginbuyappDate = queryConten.getExtName1();
      endbuyappDate = queryConten.getExtName2();
      projectId = queryConten.getExtCode3();
      projectName = queryConten.getExtName3();
      materialKindCode = queryConten.getExtCode4();
      materialKindName = queryConten.getExtName4();
      buyappStatus = queryConten.getExtCode5();
      buyappStatusName = queryConten.getExtName5();
      buyPurposeCode = queryConten.getExtCode6();
      buyPurposeName = queryConten.getExtName6();
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:
        ExtraValueContent queryContent = new ExtraValueContent();
        queryContent.setExtName1(beginbuyappDate);
        queryContent.setExtName2(endbuyappDate);
        queryContent.setExtCode3(projectId);
        queryContent.setExtName3(projectName);
        queryContent.setExtCode4(materialKindCode);
        queryContent.setExtName4(materialKindName);
        queryContent.setExtName5(buyappStatusName);
        queryContent.setExtCode5(buyappStatus);
        queryContent.setExtName6(buyPurposeName);
        queryContent.setExtCode6(buyPurposeCode);
        Intent intent = new Intent();
        intent.putExtra(EXTRA_CONTENT, queryContent);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = super.onCreateView(inflater, container, savedInstanceState);
    ButterKnife.bind(this, rootView);
    return rootView;
  }

  @Override protected int initLayoutId() {
    return R.layout.fragment_buy_apply_query;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    tvBeginregDate.setText(beginbuyappDate);
    tvEndregDate.setText(endbuyappDate);
    tvProject.setText(projectName);
    tvMaterielKind.setText(materialKindName);
    tvDataStatus.setText(buyappStatusName);
    tvBuyPurpose.setText(buyPurposeName);
  }

  @OnClick({
      R.id.ib_beginregDate, R.id.ib_endregDate, R.id.ib_project, R.id.ib_materielKind,
      R.id.ib_dataStatus, R.id.ib_buyPurpose
  }) public void onClick(View view) {
    switch (view.getId()) {
      case R.id.ib_beginregDate:
        showDialog(new BaseYMDTimeDialogFragment(), BEGINREG_DATE);
        break;
      case R.id.ib_endregDate:
        showDialog(new BaseYMDTimeDialogFragment(), ENDREG_DATE);
        break;
      case R.id.ib_project:
        getProject();
        break;
      case R.id.ib_materielKind:
        //物料类别
        volleyPostMaterial();
        break;
      case R.id.ib_dataStatus:
        //数据状态
        showDialog(new BuyApplyStatusDialogFragment(), REQUEST_STATUS);
        break;
      case R.id.ib_buyPurpose:
        //采购用途
        showDialog(BuyPurposeDialogFragment.newInstance(BuyPurposeDialogFragment.QUERY),
            REQUEST_BUYPURPOSE);
        break;
    }
  }

  private void volleyPostMaterial() {
    SimpleTreeListParams params = new SimpleTreeListParams();
    //noinspection unchecked
    ZTreeNode item = new ZTreeNode();
    item.setName("");
    item.setId("");
    params.setItem(item);
    params.setChildField("id");
    params.setParentField("pid");
    params.setTitleField("name");
    params.setTitle("物料类别");
    params.setClassOriginal(ZTreeNodeListOriginal.class);
    params.setUrl(ApiConst.URL_BUY_MATERIE_LOM);
    params.setParamsNotEmpty(
        new ParamsNotEmpty().put("memberCode", CurrentUser.newInstance(mActivity).getMemberCode())
            .put("dataStatus", "publish"));
    BaseVolleyActivity.newInstance(this, ZTreeListActivity.class, params, REQUEST_KIND);
  }

  private void getProject() {
    Intent project = new Intent(getActivity(), ProjectChooseListActivity.class);
    startActivityForResult(project, REQUEST_PROJECT);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == BEGINREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      beginbuyappDate = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvBeginregDate.setText(beginbuyappDate);
    }
    if (requestCode == ENDREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      endbuyappDate = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvEndregDate.setText(endbuyappDate);
    }
    if (requestCode == REQUEST_PROJECT) {
      ProjectCollectOriginal.ProjectCollectContent objectContent =
          (ProjectCollectOriginal.ProjectCollectContent) data.getSerializableExtra(
              ProjectChooseListFragment.EXTRA_CONTENT);
      projectId = objectContent.getId();
      projectName = objectContent.getProjectName();
      tvProject.setText(projectName);
    }
    if (requestCode == REQUEST_KIND) {

      SimpleTreeListParams params = (SimpleTreeListParams) data.getSerializableExtra(EXTRA_PARAMS);
      ZTreeNode item = ((ZTreeNode) params.getItem());
      materialKindCode = item.getId();
      materialKindName = item.getName();
      tvMaterielKind.setText(materialKindName);
    }
    if (requestCode == REQUEST_STATUS) {
      CustomerDialog dataStatus =
          (CustomerDialog) data.getSerializableExtra(BuyApplyStatusDialogFragment.EXTRA_STATUS);
      buyappStatus = dataStatus.getId();
      buyappStatusName = dataStatus.getName();
      tvDataStatus.setText(buyappStatusName);
    }
    if (requestCode == REQUEST_BUYPURPOSE) {
      //采购用途
      CustomerDialog content =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      buyPurposeCode = content.getId();
      buyPurposeName = content.getName();
      tvBuyPurpose.setText(buyPurposeName);
    }
  }

  public static BuyApplyQueryFragment newInstance(ExtraValueContent queryConten) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, queryConten);
    BuyApplyQueryFragment fragment = new BuyApplyQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }
}

